﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.AccessControl;
using System.IO;
using System.Security.Principal;

namespace HU8.Helpers.Files.Resource
{
    public class ResourceItem
    {
        public ResourceItem()
        {
        }

        public void LoadSecurityInfo()
        {
            FileSystemSecurity security;
            if (File.Exists(Path))
            {
                security = File.GetAccessControl(Path);
            }
            else
            {
                security = Directory.GetAccessControl(Path);
            }
            FileInfo fi = new FileInfo(Path);
            IsWritable = !fi.IsReadOnly;
            
            foreach (FileSystemAccessRule rule in security.GetAccessRules(true, true, typeof(NTAccount)))
            {
                string id = (string.IsNullOrEmpty(Environment.UserDomainName) ? "" : Environment.UserDomainName + "\\") + Environment.UserName;
                if (rule.IdentityReference.Value == id || rule.IdentityReference.Value == "Everyone")
                {
                    if (rule.AccessControlType == AccessControlType.Allow && rule.FileSystemRights.HasFlag(FileSystemRights.Write))
                    {
                        IsWritable = true;
                    }
                    if (rule.AccessControlType == AccessControlType.Allow && rule.FileSystemRights.HasFlag(FileSystemRights.Read))
                    {
                        IsBrowsable = true;
                    }
                }
            }
            foreach (FileSystemAccessRule rule in security.GetAccessRules(true, true, typeof(NTAccount)))
            {
                string id = (string.IsNullOrEmpty(Environment.UserDomainName) ? "" : Environment.UserDomainName + "\\") + Environment.UserName;
                if (rule.IdentityReference.Value == id || rule.IdentityReference.Value == "Everyone")
                {
                    if (rule.AccessControlType == AccessControlType.Deny && rule.FileSystemRights.HasFlag(FileSystemRights.Write))
                    {
                        IsWritable = false;
                    }
                    if (rule.AccessControlType == AccessControlType.Deny && rule.FileSystemRights.HasFlag(FileSystemRights.Read))
                    {
                        IsBrowsable = false;
                    }
                }
            }
        }


        public string GetPermission(string lang = "zh")
        {
            FileSystemSecurity security;
            if (File.Exists(Path))
            {
                security = File.GetAccessControl(Path);
            }
            else
            {
                security = Directory.GetAccessControl(Path);
            }
            string permission = "";
            permission += "<table class='fe-permissions'>";
            switch(lang){
                case "zh":
                    permission += "<thead><tr><th>用户</th><th>是否允许</th><th>权限</th></tr></thead>";
                    break;
                default:
                    permission += "<thead><tr><th>User</th><th>Allow/Disallow</th><th>Permissions</th></tr></thead>";
                    break;
            }
            permission += "<tbody>";
            foreach (FileSystemAccessRule rule in security.GetAccessRules(true, true, typeof(NTAccount)))
            {
                permission += "<tr><td>" + rule.IdentityReference.Value + "</td><td>" + rule.AccessControlType.ToString() + "</td><td>" + rule.FileSystemRights.ToString() + "</td></tr>";
            }
            permission += "</tbody>";
            permission += "</table>";
            return permission;
        }

        

        public bool Exists { get; set; } //文件是否存在
        public string Name { get; set; } //文件名
        public string RelativePath { get; set; } //相对路径
        public string Path { get; set; } //文件路径

        public string Attributes { get; set; }
        public long Size { get; set; } //大小
        public string SizeString { get { return FileHelper.SizeToString(Size); } }
        public DateTime Created { get; set; } //创建日期
        public string CreatedString { get { return Created.ToString(FileHelper.DateFormat); } }
        public DateTime LastModified { get; set; } //修改日期
        public string LastModifiedString { get { return LastModified.ToString(FileHelper.DateFormat); } }
        public DateTime LastAccessed { get; set; } //访问日期
        public string LastAccessedString { get { return LastAccessed.ToString(FileHelper.DateFormat); } }


        public string Permission { get; set; }

        public bool IsReadOnly { get; set; }
        public bool IsHidden { get; set; }
        public bool IsArchive { get; set; }
        public bool IsSystem { get; set; }

        public bool IsWritable { get; set; }
        public bool IsBrowsable { get; set; }

    }
}
